package com.xwd.servlet;

import com.xwd.pojo.Emp;
import com.xwd.service.EmpService;
import com.xwd.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName EmpServlet
 * @Description: com.com.xwd.servlet
 * @Auther: xiwd
 * @Date: 2022/2/7 - 02 - 07 - 19:02
 * @version: 1.0
 */
@WebServlet(
        name = "empservlet",
        value = {
                "/empservlet"
        }
)
public class EmpServlet extends HttpServlet {
    //properties
    private static EmpService empService=new EmpServiceImpl();


    //methods
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
        //设置参数解析字符编码格式
        req.setCharacterEncoding("UTF-8");
        //设置响应字符编码格式
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //resp.setContentType("application/json;charset=UTF-8");
        //解析请求参数
        List<Emp> emps = empService.findAll();
        //emps.forEach(e-> System.out.println(e.toString()));
        //设置request域的attribute属性信息
        req.setAttribute("emps",emps);
        //请求转发
        req.getRequestDispatcher("/showEmp.jsp").forward(req,resp);

        //获取输出流对象
//        PrintWriter writer = resp.getWriter();
//        writer.print(JSON.toJSONString(emps));
    }
}
